package com.hlx.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlx.utils.Result;
import com.hlx.entity.user.User;
import com.hlx.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
//
//    //    查询所有用户
//    @GetMapping("/selectAllUser")
//    public Result<List<User>> getAll() {
//        List<User> list = userService.list();
//        return Result.success(list, "查询成功");
//    }
//
//    //  根据账户查询用户信息
//    @PostMapping("/selectUserByAccount")
//    public Result<User> getUserByAccount(String account) {
////        log.info("方法：{}", account);
//        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(User::getAccount, account);
//        User user = userService.getOne(queryWrapper);
//        return Result.success(user, "查询成功");
//    }
//
    //  根据账户修改信息
    @PutMapping("/updateUser")
    public Result<String> updateUser(@RequestBody User user) {
        log.info("{}",user);
        if(user.getUserPassword() != null){
            user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        }
        if (!userService.updateById(user)) {
            return Result.error("系统出现bug，稍后重试");
        }
        return Result.success("修改成功！");
    }

    //   注册新用户
    @PostMapping("/registUser")
    public Result<User> addUser(@RequestBody User user) {
        boolean result = userService.save(user);
        if (result == true) {
            return Result.success(user);
        } else {
            return Result.error("系统出现bug，稍后重试");
        }
    }
//
//    //    注销用户
//    @DeleteMapping("/deleteUser")
//    public Result<User> deleteUser(User user) {
//        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(User::getAccount, user.getAccount());
//        boolean result = userService.remove(queryWrapper);
//        if (result == true) {
//            return Result.success(user, "注销成功！");
//        } else {
//            return Result.error("系统出现bug，稍后重试");
//        }
//    }

    // 用户登录
    @PostMapping("/loginUser")
    public Result<User> loginUser(@RequestBody  User us) {
        log.info("Account:{},Password{}\n",us.getUserAccount(),us.getUserPassword());
        String password = us.getUserPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserAccount, us.getUserAccount());
        User user = userService.getOne(queryWrapper);
        System.out.println(user);
        if(user == null){
            return Result.error("查无此用户");
        }
        if(!password.equals(user.getUserPassword())){
            return Result.error("密码错误");
        }
        return Result.success(user);
    }
}
